/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_05_swing.components.Comp_01_TextComponent.java]
 * Creation Date 	[22-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing.components;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * Swing文本输入组件 - JTextField / JPasswordField / JTextArea / JLabel
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Comp_01_TextComponent {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				TextInputFrame frame = new TextInputFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 文本输入窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class TextInputFrame extends JFrame {

	private static final long serialVersionUID = -3066564851070324173L;

	private JLabel usernameLabel = null;
	private JLabel passwordLabel = null;

	private JTextField usernameTextField = null;
	private JPasswordField passwordTextField = null;

	private JPanel labelPanel = null;

	private JTextArea echoArea = null;
	private JScrollPane scrollPane = null;

	private JButton insertButton = null;
	private JPanel buttonPanel = null;

	/**
	 * 构造方法
	 */
	public TextInputFrame() {
		this.setTitle("Text Input");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.setResizable(false);

		// 初始化窗体的North部分
		usernameLabel = new JLabel("username:", SwingConstants.RIGHT);
		passwordLabel = new JLabel("password:", SwingConstants.RIGHT);

		usernameTextField = new JTextField();
		passwordTextField = new JPasswordField();
		passwordTextField.setEchoChar('*');// 设置密码回显字符

		labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(2, 2));
		labelPanel.add(usernameLabel);
		labelPanel.add(usernameTextField);
		labelPanel.add(passwordLabel);
		labelPanel.add(passwordTextField);

		// 初始化窗体的South部分
		buttonPanel = new JPanel();
		insertButton = new JButton("insert");
		buttonPanel.add(insertButton);
		insertButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String username = usernameTextField.getText();
				String password = new String(passwordTextField.getPassword());

				echoArea.append("username ---> " + username);
				echoArea.append("\npassword ---> " + password);
				echoArea.append("\n");
			}
		});

		// 初始化窗体的中部
		echoArea = new JTextArea();
		scrollPane = new JScrollPane(echoArea);

		this.getContentPane().add(labelPanel, BorderLayout.NORTH);
		this.getContentPane().add(scrollPane, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
	}
}
